// 引入插件
import Vue from 'vue'
import Vuex from 'vuex'

// 引入接口
import { reqGetUserLogin, reqLaunchLogin } from '@/api/index'
// 注册插件
Vue.use(Vuex)


// 定义方法
const state = {
    token: localStorage.getItem('token'),
    name: localStorage.getItem('name')

}
const actions = {

    // 获取数据
    async getTokenDate({ commit }, query) {
        const result = await reqGetUserLogin(query)
        if (result.code === 200) {
            localStorage.setItem('token', result.data.token)
            localStorage.setItem('name', result.data.nickName)
            commit('LoginInformation', result.data)
            return true
        } else {
            console.log(result.message);
        }
    },
    // 退出
    async LogOut({ commit }) {
        const result = await reqLaunchLogin()
        if (result.code === 200) {
            localStorage.removeItem('token')
            localStorage.removeItem('name')
            commit('DeleteData', result.data)
        } else {
            console.log(result.message);
        }
    }


}
const mutations = {

    // 删除数据
    DeleteData(state) {
        state.name = ''
        state.token = ''
    }

}
const getters = {

}


export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}

